﻿Global=function(){};
Global.bindEvent=function(targetSelector,eventName, handler){
    $(targetSelector).bind(eventName,function(){
        handler(this);
    });
};
// create an namespace for javascript object
// i.e: System.Core.UI
Global.namespace=function(){
    var a=arguments, o=null, i, j, d, rt;
    for (i=0; i<a.length; ++i) {
        d=a[i].split(".");
        rt = d[0];
        eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
        for (var j=1; j<d.length; ++j) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }
};
// apply all properties of source to target
Global.apply=function(target, source){
    if(target && source && typeof source == 'object'){
        for(var p in source){
            target[p] = source[p];
        }
    }
};
// extend an methods
Global.extend = function(){
    // inline overrides
    var io = function(o){
        for(var m in o){
            this[m] = o[m];
        }
    };
    var oc = Object.prototype.constructor;

    return function(sb, sp, overrides){
        if(typeof sp == 'object'){
            overrides = sp;
            sp = sb;
            sb = overrides.constructor != oc ? overrides.constructor : function(){sp.apply(this, arguments);};
        }
        var F = function(){}, sbp, spp = sp.prototype;
        F.prototype = spp;
        sbp = sb.prototype = new F();
        sbp.constructor=sb;
        sb.superclass=spp;
        if(spp.constructor == oc){
            spp.constructor=sp;
        }
        sb.override = function(o){
            Global.override(sb, o);
        };
        sbp.override = io;
        Global.override(sb, overrides);
        sb.extend = function(o){Global.extend(sb, o);};
        return sb;
    };
}();
// override an methods
Global.override = function(origclass, overrides){
    if(overrides){
        var p = origclass.prototype;
        for(var method in overrides){
            p[method] = overrides[method];
        }
    }
};
// apply all bookmark in html template
Global.applyBookmarks=function(html){
    html=StringHandler.replace(html, "<##rootUrl>",Config.rootUrl);
    html=StringHandler.replace(html, "<##imageUrl>",Config.imageUrl);
    html=StringHandler.replace(html, "<##avatarUrl>",Config.avatarUrl);
    return html;
};
// load js file into browser
Global.loadScript=function(type, src, callback){   
    var pos=src.indexOf('?');
    var t=Global.random();
    src=src+(pos>=0?"&":"?")+ t+"="+t;
    
    var tpl='<script type="'+ type+'" src="'+ src+'" ></script>';
    
    if(callback){
        tpl='<script onload="'+ callback +'();" type="'+ type+'" src="'+ src+'" ></script>';
    }
    
    //tpl=StringHandler.format(tpl,type,src);
    window.document.write(tpl);
};
// load js file into browser with condition
Global.loadScriptIf=function(ifValue, type, src){
    var pos=src.indexOf('?');
    var t=Global.random();
    src=src+(pos>=0?"&":"?")+ t+"="+t;
    
    var tpl='<!--[if '+ifValue+']><script type="'+ type+'" src="'+ src+'" ></script><![endif]-->';
    window.document.write(tpl);
};
// load css file into browser
Global.loadCss=function(src){  
    var pos=src.indexOf('?');
    var t=Global.random();
    src=src+(pos>=0?"&":"?")+ t+"="+t;
     
    var tpl='<link rel="stylesheet" type="text/css" href="'+src+'" />';
    window.document.write(tpl);
};
// load css file into browser with condition
Global.loadCssIf = function(ifValue, src){   
    var pos=src.indexOf('?');
    var t=Global.random();
    src=src+(pos>=0?"&":"?")+ t+"="+t;
    
    var tpl='<!--[if '+ifValue+']><link rel="stylesheet" type="text/css" href="'+src+'" /><![endif]-->';
    window.document.write(tpl);
};

Global.createMask=function(dom){
    if(!dom){ return "";}
    var tpl="<div class='Mask' style='width:{0}px,height:{1}px,top:{2}px,left:{3}px'><img src='<##imageUrl/transparent.gif' alt='' />'</div>";
    return StringHandler.format(tpl,dom.width,dom.height,dom.top, dom.left);
};
Global.random=function(){
    var date=new Date();
    return date.getMilliseconds();
};

Global.createLoadingMask = function(jqDom, id){
    if(!jqDom){ return "";}
    var itemId=id;
    if(!id){
        itemId=jqDom.id+'_Loading';
    }
    var html="<div id='{4}' class='LoadingMask' style='width:{0}px;height:{1}px;left:{2}px;top:{3}px;' />";
    return StringHandler.format(
        html,
        jqDom.attr('clientWidth'),
        jqDom.attr('clientHeight'),
        jqDom.attr('offsetLeft'),
        jqDom.attr('offsetHeight'),
        itemId
    );
};

Global.showLoadingMask=function(config){
    var item=$(config.wrapper);
    var html=Global.createLoadingMask(item, config.id);
    $(html).appendTo($('body'));
};
Global.hideLoadingMask=function(id){
    $('#'+id).remove();
};